﻿<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="敏感词替换" %>

<script runat="server">

	protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
	{
		if (e.CommandName == "MoveUp")
		{
			MoveUp(int.Parse(e.CommandArgument.ToString()));
			ListView1.DataBind();
		}
		else if (e.CommandName == "MoveDown")
		{
			MoveDown(int.Parse(e.CommandArgument.ToString()));
			ListView1.DataBind();
		}
	}

	protected void ValidateSource(object sender, ServerValidateEventArgs e)
	{
		using (TDataContext db = new TDataContext())
		{
			var s = db.TSensitiveWords.FirstOrDefault(sw => sw.Source == e.Value);
			e.IsValid = (s == null);
		}
	}

	private void MoveUp(int id)
	{
		using (TDataContext db = new TDataContext())
		{
			var thisone = db.TSensitiveWords.First(sw => sw.Id == id);
			var prevone = db.TSensitiveWords.Where(sw => sw.Id < id).OrderByDescending(sw => sw.Id).FirstOrDefault();
			
			if(prevone == null)
				return;

			string temp = prevone.Source;
			prevone.Source = thisone.Source;
			thisone.Source = temp;

			temp = prevone.Destination;
			prevone.Destination = thisone.Destination;
			thisone.Destination = temp;

			db.SubmitChanges();
		}
	}

	private void MoveDown(int id)
	{
		using (TDataContext db = new TDataContext())
		{
			var thisone = db.TSensitiveWords.First(sw => sw.Id == id);
			var nextone = db.TSensitiveWords.Where(sw => sw.Id > id).OrderBy(sw => sw.Id).FirstOrDefault();

			if (nextone == null)
				return;

			string temp = nextone.Source;
			nextone.Source = thisone.Source;
			thisone.Source = temp;

			temp = nextone.Destination;
			nextone.Destination = thisone.Destination;
			thisone.Destination = temp;

			db.SubmitChanges();
		}
	}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
	<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
		ContextTypeName="Xpus.YouYTube.Data.TDataContext" EnableInsert="True" 
		EnableUpdate="True" TableName="TSensitiveWords" EnableDelete="True">
	</asp:LinqDataSource>
	<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
		DataSourceID="LinqDataSource1" InsertItemPosition="FirstItem" 
		onitemcommand="ListView1_ItemCommand">
		<LayoutTemplate>
			<table runat="server">
				<tr runat="server">
					<td runat="server">
						<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
							<tr runat="server" style="">
								<th runat="server">操作</th>
								<th runat="server">敏感词</th>
								<th runat="server">替换为</th>
							</tr>
							<tr ID="itemPlaceholder" runat="server">
							</tr>
						</table>
					</td>
				</tr>
				<tr runat="server">
					<td runat="server" style="">
					</td>
				</tr>
			</table>
		</LayoutTemplate>
		<InsertItemTemplate>
			<tr style="">
				<td>添加敏感词：
					<asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" 
						Text="保存" ValidationGroup="InsertValidationGroup" />
					<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" 
						Text="清空" CausesValidation="false" />
				</td>
				<td>
					<asp:TextBox ID="SourceTextBox" runat="server" MaxLength="128" Text='<%# Bind("Source") %>' /><br />
					<asp:RequiredFieldValidator ValidationGroup="InsertValidationGroup" runat="server" ControlToValidate="SourceTextBox" ErrorMessage="必须填写" Display="Dynamic">
					</asp:RequiredFieldValidator><asp:CustomValidator ValidationGroup="InsertValidationGroup" runat="server" ControlToValidate="SourceTextBox" ErrorMessage="已存在" Display="Dynamic" OnServerValidate="ValidateSource">
					</asp:CustomValidator>
				</td>
				<td>
					<asp:TextBox ID="DestinationTextBox" runat="server" MaxLength="128" 
						Text='<%# Bind("Destination") %>' /><br />
					<asp:RequiredFieldValidator ValidationGroup="InsertValidationGroup" runat="server" ControlToValidate="DestinationTextBox" ErrorMessage="必须填写" Display="Dynamic">
					</asp:RequiredFieldValidator>
				</td>
			</tr>
		</InsertItemTemplate>
		<EmptyDataTemplate>
			<table runat="server" style="">
				<tr>
					<td>暂无数据。</td>
				</tr>
			</table>
		</EmptyDataTemplate>
		<EditItemTemplate>
			<tr style="">
				<td>
					<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" 
						Text="保存" ValidationGroup="EditValidationGroup" />
					<asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" 
						Text="取消" CausesValidation="false" />
				</td>
				<td>
					<asp:TextBox ID="SourceTextBox" runat="server" MaxLength="128" Text='<%# Bind("Source") %>' /><br />
					<asp:RequiredFieldValidator ValidationGroup="EditValidationGroup" runat="server" ControlToValidate="SourceTextBox" ErrorMessage="必须填写" Display="Dynamic">
					</asp:RequiredFieldValidator><asp:CustomValidator ValidationGroup="EditValidationGroup" runat="server" ControlToValidate="SourceTextBox" ErrorMessage="已存在" Display="Dynamic" OnServerValidate="ValidateSource">
					</asp:CustomValidator>
				</td>
				<td>
					<asp:TextBox ID="DestinationTextBox" runat="server" MaxLength="128"
						Text='<%# Bind("Destination") %>' /><br />
					<asp:RequiredFieldValidator ValidationGroup="EditValidationGroup" runat="server" ControlToValidate="DestinationTextBox" ErrorMessage="必须填写" Display="Dynamic"></asp:RequiredFieldValidator>
				</td>
			</tr>
		</EditItemTemplate>
		<ItemTemplate>
			<tr style="">
				<td>
					<asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="修改" />
					<asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" OnClientClick="return confirm('该操作无法撤销，是否继续？')" />
					<asp:LinkButton ID="MoveUpButton" runat="server" CommandName="MoveUp" CommandArgument='<%# Eval("Id") %>' Text="上移" />
					<asp:LinkButton ID="MoveDownButton" runat="server" CommandName="MoveDown" CommandArgument='<%# Eval("Id") %>' Text="下移" />
				</td>
				<td>
					<asp:Label ID="SourceLabel" runat="server" Text='<%# Eval("Source") %>' />
				</td>
				<td>
					<asp:Label ID="DestinationLabel" runat="server" 
						Text='<%# Eval("Destination") %>' />
				</td>
			</tr>
		</ItemTemplate>
	</asp:ListView>
</asp:Content>

